package de.tum.in.far.threedui.superbrickball.engine.physics;

import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import de.tum.in.far.threedui.superbrickball.engine.physics.models.PlaneModel;
import de.tum.in.far.threedui.superbrickball.engine.physics.models.SphereModel;

public class WorldFactory {

	public static World createSimpleWorld() {
		World world = new World();

		generateSimpleWorldBall(world);
		generateSimpleWorldTargets(world);
		generateSimpleWorldWalls(world);

		world.CLOCK_PERIOD = 50;
		world.REPULSION = 0.0000425f;
		world.MIN_FORCE_THRESHOLD = 0.000025f;
		world.MAX_SPEED = 0.01255f;
		
		world.controllerDimension = 0.013f;

		return world;
	}

	private static void generateSimpleWorldWalls(World world) {
		
		world.zOffset = -.5f;
		float zOffset = world.zOffset;
		
		float worldHeight = 0.1925f;
		float worldWidth = worldHeight;
		float worldDepth = worldWidth + 0.51f;
		
		world.height = worldHeight*1.5f;
		world.width = worldWidth*1.5f;
		world.depth = worldDepth*1.5f;
		
		world.wallModels.put("R", new PlaneModel(new Vector3f(-1,0,0), new Point3f(worldWidth/2, 0,zOffset)));
		world.wallModels.put("L", new PlaneModel(new Vector3f(1,0,0), new Point3f(-worldWidth/2, 0,zOffset)));
		
		world.wallModels.put("T", new PlaneModel(new Vector3f(0,-1,0), new Point3f(0,worldHeight/2,zOffset)));
		world.wallModels.put("D", new PlaneModel(new Vector3f(0,1,0), new Point3f(0,-worldHeight/2,zOffset)));
//		
		world.wallModels.put("F", new PlaneModel(new Vector3f(0,0,-1), new Point3f(0, 0,-worldDepth/2 + zOffset)));
		world.wallModels.put("B", new PlaneModel(new Vector3f(0,0,1), new Point3f(0, 0,worldDepth/2 )));
	}

	private static void generateSimpleWorldTargets(World world) {
		world.zOffset = -0.5f;
		float zOffset = world.zOffset;
		
		int ballRows = 5;
		int ballColumns = 5;
		int ballLayers = 3;
		float targetRadius = 0.0058103f;
		
		float step = world.ballRadius + 0.013f;
		Vector3f targetsCenter = new Vector3f(0, 0, -0.2f);

		for (int i = -ballRows / 2; i <= ballRows / 2; i++) {
			for (int j = -ballColumns / 2; j <= ballColumns / 2; j++) {
				for (int k = -ballLayers / 2; k <= ballLayers / 2; k++) {
					Vector3f pos = new Vector3f(i * step, j * step, k * step + zOffset);
					pos.add(targetsCenter);
					

					SphereModel sm = new SphereModel(pos, targetRadius + (float)Math.random()/100);
					world.targetModels.put(""+i+","+j+","+k, sm);
				}
			}
		}
	}

	private static void generateSimpleWorldBall(World world) {
		world.ballRadius = 0.012f;
		world.ballModel.setPosition(new Vector3f(0, 0, -0.5f));
		world.ballModel.setRadius(world.ballRadius);
		world.ballVelocity.set(0, 0, 0.001f);
	}

}
